//点击返回顶部
let scrolltop = document.documentElement.scrollTop
$("#foot ul .li3").click(function () {
    $("html,body").animate({ scrollTop: 0 }, 500)
})
//点击返回图标返回首页
$("#header .fh").click(function(){
    window.location.href="../index.html"
})
//请求数据， 初始加载页面时候默认发送一个请求
fetch("http://chst.vip:1234/api/getmoneyctrl")
    .then(body => body.json())
    .then(res => {
        let resultarr = res.result
        let str = ""
        resultarr.forEach(item => {
            str += `
        <li>
        ${item.productImgSm}
        <p class="jieshao" productId="${item.productId}">${item.productName}<span class="pinkage">${item.productPinkage}</span></p>
        <p class="biaoqian">${item.productFrom}|${item.productTime}<span class="pinlun" id="x"><span class="iconfont icon-pinglun"></span>${item.productComCount}</span></p>
        </li>
        `
        })
        $("#main .lis").html(str)

    })
//---------------------------------
//给中间的下拉菜单添加onchange时间，监听当前option获取对应页，发请求传入对应页传query传参拿去对应页数据，渲染页面结构
//给上一页和下一页注册点击事件，点击改变下拉菜单的option显示


$(".up").click(function () {
    let s_option = $("#select option:selected")
    let s_index = s_option.val()
    $("#select option:selected").attr("selected",false)
    s_index = s_index-1
    //设置value值为s_index的option为选中状态
    //.trigger("change")用来触发change事件，不然只凭js代码改变的状态非用户改变的状态无法触发change事件
    $("#select").val(s_index).trigger("change")
})
$(".down").click(function () {
    let s_option = $("#select option:selected")
    let s_index = s_option.val()
    $("#select option:selected").attr("selected",false)
    s_index = parseInt(s_index)+1
    //设置value值为s_index的option为选中状态
    //.trigger("change")用来触发change事件，不然只凭js代码改变的状态非用户改变的状态无法触发change事件
    $("#select").val(s_index).trigger("change")
})
//-------------------------------------------------
//首次加载禁用上一页
$(".up").attr("disabled",true)
$("#select").change(function () {
    // console.log(3);
    //获取选择中的option
    let s_option = $("#select option:selected")
    //拿到value值，此为要传入请求的索引值
    let s_index = s_option.val()
    console.log(s_index);
    //按钮禁用
    if(s_index>0&&s_index<13){
        $(".up").attr("disabled",false)
        $(".down").attr("disabled",false)
    }else if(s_index==13){
        $(".down").attr("disabled",true)
    }else if(s_index==0){
        $(".up").attr("disabled",true)
    }
    // console.log(s_index);
    fetch(`http://chst.vip:1234/api/getmoneyctrl?pageid=${s_index}`)
        .then(body => body.json())
        .then(res => {
            let resultarr = res.result
            let str = ""
            resultarr.forEach(item => {
                str += `
        <li>
        ${item.productImgSm}
        <p class="jieshao" productId="${item.productId}">${item.productName}<span class="pinkage">${item.productPinkage}</span></p>
        <p class="biaoqian">${item.productFrom}|${item.productTime}<span class="pinlun" id="x"><span class="iconfont icon-pinglun"></span>${item.productComCount}</span></p>
        </li>
        `
            })
            $("#main .lis").html(str)
        })
})
//根据lis页面点击跳转到详情页面的local href 并且 拼接?productid=x
$("#main .lis").on("click","li img",function(e){
    console.log(1);
    let _this = e.target
    let clickid = $(_this).next().attr("productId")
    console.log(clickid);
    //跳转页面，动态拼接id
    window.location.href=`sqk_detail.html?productId=${clickid}`
})
